
using UnityEngine;
using System.Collections;
using System.Collections.Generic;

internal class PrefabPool
{
	UnityEngine.Object mPrefab; 
	
	List<UnityEngine.Object> spawnedList;
	List<UnityEngine.Object> despawnedList;
	
	public PrefabPool(UnityEngine.Object prefab)
	{
		mPrefab = prefab;
		Clear();
	}
	
	public UnityEngine.Object GetPrefab()
	{
		return mPrefab;
	}
	
	public void Clear()
	{
		spawnedList = new List<UnityEngine.Object>();
		
		if (null != despawnedList)
		{
			foreach (UnityEngine.Object obj in despawnedList)
			{
				if (Application.isEditor)
				{
					UnityEngine.Object.DestroyImmediate(obj);
				}
				else
				{
					UnityEngine.Object.Destroy(obj);
				}
			}
		}
		despawnedList = new List<UnityEngine.Object>();
	}
	
	public UnityEngine.Object Spawn()
	{
		UnityEngine.Object ret;
		
		if (despawnedList.Count > 0)
		{
			ret = despawnedList[despawnedList.Count - 1];
			despawnedList.RemoveAt(despawnedList.Count - 1);
		}
		else
		{
			ret = UnityEngine.Object.Instantiate(mPrefab);
		}
		
		spawnedList.Add(ret);
		
		return ret;
	}
	
	public void Despawn(UnityEngine.Object obj)
	{
		spawnedList.Remove(obj);
		despawnedList.Add(obj);
	}
	
	public bool IsObjectSpawnedFromPool(UnityEngine.Object obj)
	{
		int foundIndex = spawnedList.IndexOf(obj);
		if(0 > foundIndex)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}